/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package core;

import java.util.List;
import model.Department;
import model.Employee;
import org.hibernate.Query;
import org.hibernate.Session;
import util.ProjetHibernateUtil;

/**
 *
 * @author phinguyen
 */
public class ResearchCore {

    private final Session session;

    public ResearchCore(Session currentSession) {
        session = currentSession;
    }

    public Department researchDepartmentById(String id) {
        return (Department) session.get(Department.class, id);
    }

    public Department researchDepartmentByName(String name) {
        for (Department department : this.getAllDepartment()) {
            if (department.getDeptname().equals(name)) {
                return department;
            }
        }
        return null;
    }

    /**
     * return all departments. It will call an HQL query in Hibernate
     * Department's mapping
     *
     * @return
     */
    public List<Department> getAllDepartment() {
        Query query = session.getNamedQuery("HQL_ALL_DEPARTMENT");
        List<Department> listAllDepartment = query.list();
        return listAllDepartment;
    }

    /**
     * return all employees of a department
     *
     * @param department
     * @return
     */
    public List<Employee> getAllEmployeesOfDepartment(Department department) {
        Query query = session.createQuery("from Department where " + department.toString());
        List result = query.list();
        return result;
    }

    public List<Employee> getAllEmployees() {
        Query query = session.getNamedQuery("HQL_ALL_EMPLOYEE");
        List result = query.list();
        return result;
    }

    /**
     * return all emails of employees
     *
     * @return
     */
    public List<String> getAllEmailEmployee() {
        Query query = session.getNamedQuery("HQL_ALL_EMAILEMPLOYEE");
        List result = query.list();
        return result;
    }

    public Employee getEmployeeByMail(String mail) {
        for (Employee employee : this.getAllEmployees()) {
            if (employee.getEmail().equals(mail)) {
                return employee;
            }
        }
        return null;
    }
}
